package com.fantasy.selenium;

import java.io.BufferedReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import com.fantasy.file.GetFolderAbsPath;
import com.fantasy.file.ReadTxt;
import com.fantasy.mysql.GetConn;
import com.fantasy.mysql.SqlUtil;

public class SeleniumTest {

	private static WebDriver driver = new FirefoxDriver();
	private static ArrayList<String> domainList;
	
	public static void main(String[] args) {
		getUrl("http://www.csdn.net/");
		driverMax(driver);
		//box enterprise
		WebElement txtbox = driver.findElement(By.className("enterprise"));
		List<WebElement> buttons = txtbox.findElements(By.tagName("a"));
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(buttons.size() > 0){
			buttons.get(0).click();
		}
	}
	
	public static void getDomainMessage(){
		getUrl("https://nic.xin/whois?t=whois-web-hichina-com4143881112&c=sss");
		//find("bbkb");
		String filePath = GetFolderAbsPath.getPath() + "domainList.txt";
		//debian 需要
		filePath = filePath.replace("\\", "/");
		readTxt(filePath);
		int i = 1;
		for (String domain : domainList) {
			find(domain.replace(".xin", ""));
			i = i + 1;
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if(i > 2555){
				i = 1;
				driver.quit();
				//break;
				driver = null;
				driver = new FirefoxDriver();
				getUrl("https://nic.xin/whois?t=whois-web-hichina-com4143881112&c=sss");
			}
		}
		//System.out.println(driver.getTitle());
		//driver.close();
	}
	
	public static void readTxt(String path){
		domainList = new ArrayList<String>();
		BufferedReader bufReader = ReadTxt.GetBufferReader(path);
		String data;
		try {
			data = bufReader.readLine();
			//System.out.println("startTxt:" + data);
			while(null != data){
				domainList.add(data);
				data = bufReader.readLine();
			}
			bufReader.close();
		} catch (IOException e) {
			try {
				bufReader.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
	}
	
	// id who-body 代表是谁注册
	/**
	 * 获取注册信息
	 * */
	private static void getMsg(){
		WebElement txtbox = driver.findElement(By.id("who-body"));
		txtbox.findElement(By.tagName("pre"));
		//System.out.println(txtbox.getText());
		String data = txtbox.getText();
		String userName = parseMsg(data, "Registrant Name:");
		String userMail = parseMsg(data, "Registrant Email:");
		String userPhone = parseMsg(data, "Registrant Phone:");
		String regOrg = parseMsg(data, "Registrant Organization:");
		String adminName = parseMsg(data, "Admin Name:");
		String adminOrg = parseMsg(data, "Admin Organization:");
		String adminPhone = parseMsg(data, "Admin Phone:");
		String adminMail = parseMsg(data, "Admin Email:");
		String domain = parseMsg(data, "Domain Name:");
		String startDate = parseMsg(data, "Creation Date:");
		String endDate = parseMsg(data, "Registry Expiry Date:");
		executeSql(domain, userName, userMail, userPhone , regOrg ,adminName ,adminOrg , adminPhone ,adminMail , startDate, endDate);
	}
	
	private static void executeSql(String domain,String userName,String userMail,String userPhone,
			String regOrg , String adminName , String adminOrg , String adminPhone , String adminMail, String startDate, String endDate){
		String sql = "replace INTO `xindomain`.`xindata` (`regUser`, `domain`, `regmail`, `regphone` , `regOrg` ,`adminName` ,`adminOrg` , `adminPhone` ,`adminMail` , `startDate`, `endDate`) "
					+ "VALUES ('" + userName + "', '" + domain
					+ "', '" + userMail + "', '" + userPhone + "','" + regOrg + "','" + adminName + "','" + adminOrg + "','" + adminPhone + "','" + adminMail + "','" + startDate + "','" + endDate + "')";
		//System.out.println("exec : " + sql);
		
		Connection conn = GetConn.getConn("106.14.35.200", "xindomain", "fantasy", "88888888");
		//Connection conn = GetConn.getConn("localhost", "xindomain", "root", "12345678");
		
		try {
			SqlUtil.executeSql(conn , sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private static String parseMsg(String data , String fieldName){
		int indexStart = data.indexOf(fieldName);
		if(indexStart != -1){
			int endIndex = data.indexOf("\n", indexStart);
			if(endIndex != -1){
				return data.substring(indexStart + fieldName.length(), endIndex).trim();
			}
		}
		System.out.println("没有找到" + fieldName);
		return "";
	}
	
	private static void find(String domain){
		WebElement txtbox = driver.findElement(By.className("doamin-text"));
		txtbox.sendKeys(domain);
		txtbox = driver.findElement(By.className("domain-btn"));
		txtbox.click();
		if(isReg()){
			getMsg();
		}
	}
	
	
	// class name 代表是否注册 domain-result
	/**
	 * 判断是否注册
	 * */
	private static boolean isReg(){
		WebElement txtbox = driver.findElement(By.className("domain-result"));
		if (txtbox.getText().contains("已被注册")) {
			System.out.println(txtbox.getText());
			return true;
		} else {
			System.out.println(txtbox.getText());
			return false;
		}
	}
	
	/**
	 * 浏览器最大化
	 * */
	public static void driverMax(WebDriver driver){
		driver.manage().window().maximize();
	}
	
	/**
	 * 打开网址
	 * */
	public static void getUrl(String url){
		driver.get(url);
	}

}